﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace GUI
{
    public partial class frmCapNhatMonAn : DevComponents.DotNetBar.Office2007Form
    {
        frmMain formCha = new frmMain();
        public frmCapNhatMonAn(frmMain main)
        {
            InitializeComponent();
            formCha = main;
        }

        public NHANVIEN nv = new NHANVIEN();
        public List<NGUYENVATLIEU> arrNVL = NguyenVatLieuBUS.layDSNVL();
        public bool loadXongForm = false;
        public bool hoanTat = false;
        public List<MONAN> arrMA = MonAnBUS.layDSMA();

        public void khoiTao()
        {
            lblNhanVien.Text = nv.TenNV;

            cbbMonAn.DataSource = arrMA;
            cbbMonAn.DisplayMember = "TenMonAn";
            cbbMonAn.ValueMember = "MaMonAn";

            MONAN monan = MonAnBUS.layMonAn(int.Parse(cbbMonAn.SelectedValue.ToString()));
            txtTenMonAn.Text = monan.TenMonAn;

            cbbNVL.DataSource = arrNVL;
            cbbNVL.DisplayMember = "TenNVL";
            cbbNVL.ValueMember = "MaNVL";

            grvNVLCT.Refresh();

            grvNVLCT.DataSource = MonAnBUS.layNVLMonAnDauTien();
            grvNVLCT.Columns[0].Visible = false;

            txtSoLuong.Text = "";
        }

        private void frmCapNhatMonAn_Load(object sender, EventArgs e)
        {
            khoiTao();
            loadXongForm = true;
        }

        private void cbbNVL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadXongForm)
            {
                NGUYENVATLIEU nvl = NguyenVatLieuBUS.layNVL(int.Parse(cbbNVL.SelectedValue.ToString()));
                lblDonViTinh.Text = nvl.DonViTinh;
                txtSoLuong.Text = "";
            }
        }

        private void cbbMonAn_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadXongForm)
            {
                MONAN monan = MonAnBUS.layMonAn(int.Parse(cbbMonAn.SelectedValue.ToString()));
                txtTenMonAn.Text = monan.TenMonAn;
                grvNVLCT.Refresh();
                grvNVLCT.DataSource = MonAnBUS.layNVLMonAn(monan.MaMonAn);
                grvNVLCT.Columns[0].Visible = false;
                txtSoLuong.Text = "";
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            if (!hoanTat)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có thực sự muốn tắt màn hình thêm món ăn?", "Xác nhận", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    TabControl tab = (TabControl)(formCha.Controls["tabControlMain"]);
                    int index = KiemTraTonTai(tab, "Cập nhật món ăn");
                    tab.TabPages.RemoveAt(index);
                }
            }
            else
            {
                TabControl tab = (TabControl)(formCha.Controls["tabControlMain"]);
                int index = KiemTraTonTai(tab, "Cập nhật món ăn");
                tab.TabPages.RemoveAt(index);
            }
        }
    
        private void btnXoaTruongDL_Click(object sender, EventArgs e)
        {
            if (!hoanTat)
            {
                DialogResult dialogResult = MessageBox.Show("Món ăn chưa được lưu cập nhật. Bạn có thực sự muốn dừng?", "Xác nhận", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    //trả lại ban đầu của món ăn đó, load dl món ăn từ cbb
                    MONAN monan = MonAnBUS.layMonAn(int.Parse(cbbMonAn.SelectedValue.ToString()));
                    txtTenMonAn.Text = monan.TenMonAn;
                    grvNVLCT.Refresh();
                    grvNVLCT.DataSource = MonAnBUS.layNVLMonAn(monan.MaMonAn);
                    grvNVLCT.Columns[0].Visible = false;
                    txtSoLuong.Text = "";
                }
            }
            else
                khoiTao();
        }

        private void btnCapNhatMonAn_Click(object sender, EventArgs e)
        {
            DataTable dt = ((DataTable)grvNVLCT.DataSource).Copy();
            if (dt.Rows.Count != 0)
            {
                if (txtTenMonAn.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập tên món ăn");
                    txtTenMonAn.Focus();
                    return;
                }

                MONAN monan = new MONAN();
                monan.TenMonAn = txtTenMonAn.Text;
                monan.MaMonAn = int.Parse(cbbMonAn.SelectedValue.ToString());

                foreach (DataRow row in dt.Rows)
                {
                    DS_NVL_MONAN nvl_ma = new DS_NVL_MONAN();
                    nvl_ma.MaMonAn = monan.MaMonAn;
                    nvl_ma.MaNVL = int.Parse(row[0].ToString());
                    nvl_ma.SoLuongNVLCan = double.Parse(row[2].ToString());
                    monan.DS_NVL_MONAN.Add(nvl_ma);
                }

                bool kq = MonAnBUS.capNhatMonAn(monan);
                if (kq)
                {
                    MessageBox.Show("Cập nhật món ăn thành công");
                    hoanTat = true;
                    khoiTao();
                }
                else
                {
                    MessageBox.Show("Cập nhật món ăn không thành công. Vui lòng thử lại.");
                    hoanTat = true;
                    khoiTao();
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập nguyên vật liệu cần thiết cho món ăn");
            }
        }

        private void btnThêmNVLCT_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text != "")
            {
                DataTable dt = ((DataTable)grvNVLCT.DataSource).Copy();
                
                bool trungNVL = false;

                foreach (DataRow row in dt.Rows)
                {
                    if (row[1] == cbbNVL.Text)
                    {
                        row[2] = txtSoLuong.Text;
                        trungNVL = true;
                        break;
                    }
                }
                if (!trungNVL)
                {
                    DataRow dr = dt.NewRow();
                    NGUYENVATLIEU nvl = NguyenVatLieuDAO.layNVL(int.Parse(cbbNVL.SelectedValue.ToString()));
                    dr[0] = nvl.MaNVL;
                    dr[1] = nvl.TenNVL;
                    dr[2] = txtSoLuong.Text;
                    dt.Rows.Add(dr);
                }

                grvNVLCT.DataSource = null;
                grvNVLCT.DataSource = dt;
                grvNVLCT.Columns[0].Visible = false;
            }
        }

        private void grvNVLCT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (grvNVLCT.CurrentCell != null && grvNVLCT.CurrentCell.Value != null)
                {
                    cbbNVL.Text = grvNVLCT.Rows[grvNVLCT.CurrentRow.Index].Cells[1].Value.ToString();
                    txtSoLuong.Text = grvNVLCT.Rows[grvNVLCT.CurrentRow.Index].Cells[2].Value.ToString();
                }
            }

        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete && e.KeyChar != ',';
        }

        static int KiemTraTonTai(TabControl TabControlName, string TabName)
        {
            int temp = -1;
            for (int i = 0; i < TabControlName.TabPages.Count; i++)
            {
                if (TabControlName.TabPages[i].Text == TabName)
                {
                    temp = i;
                    break;
                }
            }
            return temp;
        }
    }
}